// pages/myself/send/EditCoupon.js
let network = null;
let utils = null;
let api = null;
var Zan = require('../../../dist/index');
let WxNotificationCenter = require('../../../utils/WxNotificationCenter.js');
const app = getApp();
Page(Object.assign({}, Zan.TopTips, Zan.Toast,{

  /**
   * 页面的初始数据
   */
  data: {
    isSubmit: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    network = new app.NetWorkUtils();
    utils = new app.Utils();
    api = new app.Api();
    // 注册列表刷新事件
    WxNotificationCenter.addNotification("RefreshNotification", this.refreshNotification, this)
    this.setData({
      BrandId: options.BrandId,
      couponId: options.couponId,
      editgotype: options.editgotype ? options.editgotype:0
    })
    var that = this
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        network._post_json(that, api.couponDetails, { couponId: options.couponId, lat: res.latitude, lng: res.longitude }, success);
      }
    })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    WxNotificationCenter.removeNotification("RefreshNotification", this)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  //选择日期
  bindDateChange: function (e) {
    var startorend = e.currentTarget.dataset.type;
    if (startorend == 1) {
      if (this.data.enddate && this.data.enddate < e.detail.value) {
        this.setData({
          startdate: ''
        })
        this.showZanTopTips(this.prompt, '开始时间不能大于结束时间');
      } else {
        this.setData({
          startdate: e.detail.value
        })
      }
    } else {
      if (this.data.startdate && this.data.startdate > e.detail.value) {
        this.setData({
          enddate: ''
        })
        this.showZanTopTips(this.prompt, '结束时间不能小于开始时间');
      } else {
        this.setData({
          enddate: e.detail.value
        })
      }

    }
    isSubmit(this)

  },
  /**
  * 选择适用门店
  */
  goChooseStore: function (e) {
    if (utils.canTap(e)) {
      wx.navigateTo({
        url: '/pages/myself/send/ChooseStore?BrandId=' + this.data.BrandId + '&selected=' + JSON.stringify(this.data.selected_storesarr)
      })
    }
  },
  //上传优惠券图片
  uploadimg: function (e) {
    var that = this;
    var choose_type = e.currentTarget.dataset.type;
    wx.chooseImage({
      count: 1,
      success: function (res) {
        var tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: api.upload,
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {
          },
          success: function (res) {
            var upimg = JSON.parse(res.data);
            that.setData({
              uploadimg: upimg.data
            })
            isSubmit(that)
          }
        })
      },
    })

  },
  //提交编辑优惠券
  formSubmit: function (e) {
    this.setData({
      name: e.detail.value.name,
      comment: e.detail.value.comment
    })

    var postdata = {
      image: this.data.uploadimg ? this.data.uploadimg : this.data.brands.image,
      id: this.data.couponId,
      name: e.detail.value.name,
      startTime: this.data.startdate,
      endTime: this.data.enddate,
      useTime: e.detail.value.useTime,
      comment: e.detail.value.comment,
      useRange: this.data.selected_stores ? this.data.selected_stores : this.data.brands.useRange,
      brandId: this.data.BrandId
    }
    isSubmit(this)
    if (this.data.isSubmit) {
      network._post_json(this, api.editCoupon, postdata, formSuccess);
    } else {
      this.showZanToast('请填写完整信息')
    }

  },
  /**
* 刷新地址
*/
  refreshNotification: function () {
    this.setData({
      selected_stores: wx.getStorageSync('selected_stores'),
      selected_storesarr: wx.getStorageSync('selected_stores').split(",")
    })
  }
}))

var success = function(that,res){
  that.setData({
    startdate: utils.formatDate02(new Date(res.data.data.startTime)),
    enddate: utils.formatDate02(new Date(res.data.data.endTime))
  })
  if(res.data.data){
    that.setData({
      brands: res.data.data,
      selected_storesarr: res.data.data.useRange.split(",")
    })
    
  }
  
}

var isSubmit = function (that) {
  if ((that.data.brands.image || that.data.uploadimg) && that.data.name && that.data.startdate && that.data.enddate && (that.data.brands.useRange || that.data.selected_storesarr.length > 0) && that.data.comment.length > 0) {
    that.setData({
      isSubmit: true
    })
  } else {
    that.setData({
      isSubmit: false
    })
  }
}

var formSuccess = function(that,res){
  that.showZanToast(res.data.data)
  if (that.data.editgotype==0){
    WxNotificationCenter.postNotificationName("RefreshOnCoupon", 3);
  }else{
    WxNotificationCenter.postNotificationName("RefreshDetailsCoupon");
  }
  wx.navigateBack();
}